

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>pathlib &mdash; NLP Architect by Intel® AI Lab 0.5.2 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
        <script type="text/javascript" src="../_static/language_data.js"></script>
        <script type="text/javascript" src="../_static/install.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/nlp_arch_theme.css" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:100,900" type="text/css" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html">
          

          
            
            <img src="../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../quick_start.html">Quick start</a></li>
<li class="toctree-l1"><a class="reference internal" href="../installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorials.html">Jupyter Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../model_zoo.html">Model Zoo</a></li>
</ul>
<p class="caption"><span class="caption-text">NLP/NLU Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tagging/sequence_tagging.html">Sequence Tagging</a></li>
<li class="toctree-l1"><a class="reference internal" href="../sentiment.html">Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../bist_parser.html">Dependency Parsing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intent.html">Intent Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../lm.html">Language Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../information_extraction.html">Information Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../transformers.html">Transformers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../archived/additional.html">Additional Models</a></li>
</ul>
<p class="caption"><span class="caption-text">Optimized Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../quantized_bert.html">Quantized BERT</a></li>
<li class="toctree-l1"><a class="reference internal" href="../transformers_distillation.html">Transformers Distillation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../sparse_gnmt.html">Sparse Neural Machine Translation</a></li>
</ul>
<p class="caption"><span class="caption-text">Solutions</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../absa_solution.html">Aspect Based Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../term_set_expansion.html">Set Expansion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../trend_analysis.html">Trend Analysis</a></li>
</ul>
<p class="caption"><span class="caption-text">For Developers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../generated_api/nlp_architect_api_index.html">nlp_architect API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../developer_guide.html">Developer Guide</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">NLP Architect by Intel® AI Lab</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">Module code</a> &raquo;</li>
        
      <li>pathlib</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for pathlib</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">fnmatch</span>
<span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">import</span> <span class="nn">io</span>
<span class="kn">import</span> <span class="nn">ntpath</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">posixpath</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">_collections_abc</span> <span class="kn">import</span> <span class="n">Sequence</span>
<span class="kn">from</span> <span class="nn">errno</span> <span class="kn">import</span> <span class="n">EINVAL</span><span class="p">,</span> <span class="n">ENOENT</span><span class="p">,</span> <span class="n">ENOTDIR</span><span class="p">,</span> <span class="n">EBADF</span><span class="p">,</span> <span class="n">ELOOP</span>
<span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="n">attrgetter</span>
<span class="kn">from</span> <span class="nn">stat</span> <span class="kn">import</span> <span class="n">S_ISDIR</span><span class="p">,</span> <span class="n">S_ISLNK</span><span class="p">,</span> <span class="n">S_ISREG</span><span class="p">,</span> <span class="n">S_ISSOCK</span><span class="p">,</span> <span class="n">S_ISBLK</span><span class="p">,</span> <span class="n">S_ISCHR</span><span class="p">,</span> <span class="n">S_ISFIFO</span>
<span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="kn">import</span> <span class="n">quote_from_bytes</span> <span class="k">as</span> <span class="n">urlquote_from_bytes</span>


<span class="n">supports_symlinks</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;nt&#39;</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">nt</span>
    <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">getwindowsversion</span><span class="p">()[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span>
        <span class="kn">from</span> <span class="nn">nt</span> <span class="kn">import</span> <span class="n">_getfinalpathname</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">supports_symlinks</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">_getfinalpathname</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">nt</span> <span class="o">=</span> <span class="kc">None</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s2">&quot;PurePath&quot;</span><span class="p">,</span> <span class="s2">&quot;PurePosixPath&quot;</span><span class="p">,</span> <span class="s2">&quot;PureWindowsPath&quot;</span><span class="p">,</span>
    <span class="s2">&quot;Path&quot;</span><span class="p">,</span> <span class="s2">&quot;PosixPath&quot;</span><span class="p">,</span> <span class="s2">&quot;WindowsPath&quot;</span><span class="p">,</span>
    <span class="p">]</span>

<span class="c1">#</span>
<span class="c1"># Internals</span>
<span class="c1">#</span>

<span class="c1"># EBADF - guard against macOS `stat` throwing EBADF</span>
<span class="n">_IGNORED_ERROS</span> <span class="o">=</span> <span class="p">(</span><span class="n">ENOENT</span><span class="p">,</span> <span class="n">ENOTDIR</span><span class="p">,</span> <span class="n">EBADF</span><span class="p">,</span> <span class="n">ELOOP</span><span class="p">)</span>

<span class="n">_IGNORED_WINERRORS</span> <span class="o">=</span> <span class="p">(</span>
    <span class="mi">21</span><span class="p">,</span>  <span class="c1"># ERROR_NOT_READY - drive exists but is not accessible</span>
    <span class="mi">1921</span><span class="p">,</span>  <span class="c1"># ERROR_CANT_RESOLVE_FILENAME - fix for broken symlink pointing to itself</span>
<span class="p">)</span>

<span class="k">def</span> <span class="nf">_ignore_error</span><span class="p">(</span><span class="n">exception</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">exception</span><span class="p">,</span> <span class="s1">&#39;errno&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">in</span> <span class="n">_IGNORED_ERROS</span> <span class="ow">or</span>
            <span class="nb">getattr</span><span class="p">(</span><span class="n">exception</span><span class="p">,</span> <span class="s1">&#39;winerror&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">in</span> <span class="n">_IGNORED_WINERRORS</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_is_wildcard_pattern</span><span class="p">(</span><span class="n">pat</span><span class="p">):</span>
    <span class="c1"># Whether this pattern needs actual matching using fnmatch, or can</span>
    <span class="c1"># be looked up directly as a file.</span>
    <span class="k">return</span> <span class="s2">&quot;*&quot;</span> <span class="ow">in</span> <span class="n">pat</span> <span class="ow">or</span> <span class="s2">&quot;?&quot;</span> <span class="ow">in</span> <span class="n">pat</span> <span class="ow">or</span> <span class="s2">&quot;[&quot;</span> <span class="ow">in</span> <span class="n">pat</span>


<span class="k">class</span> <span class="nc">_Flavour</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A flavour implements a particular (platform-specific) set of path</span>
<span class="sd">    semantics.&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">join</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sep</span><span class="o">.</span><span class="n">join</span>

    <span class="k">def</span> <span class="nf">parse_parts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parts</span><span class="p">):</span>
        <span class="n">parsed</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">sep</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sep</span>
        <span class="n">altsep</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">altsep</span>
        <span class="n">drv</span> <span class="o">=</span> <span class="n">root</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="n">it</span> <span class="o">=</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">parts</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">part</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">altsep</span><span class="p">:</span>
                <span class="n">part</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">altsep</span><span class="p">,</span> <span class="n">sep</span><span class="p">)</span>
            <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">rel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">splitroot</span><span class="p">(</span><span class="n">part</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">sep</span> <span class="ow">in</span> <span class="n">rel</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">rel</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">sep</span><span class="p">)):</span>
                    <span class="k">if</span> <span class="n">x</span> <span class="ow">and</span> <span class="n">x</span> <span class="o">!=</span> <span class="s1">&#39;.&#39;</span><span class="p">:</span>
                        <span class="n">parsed</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">intern</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">rel</span> <span class="ow">and</span> <span class="n">rel</span> <span class="o">!=</span> <span class="s1">&#39;.&#39;</span><span class="p">:</span>
                    <span class="n">parsed</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">intern</span><span class="p">(</span><span class="n">rel</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">drv</span> <span class="ow">or</span> <span class="n">root</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">drv</span><span class="p">:</span>
                    <span class="c1"># If no drive is present, try to find one in the previous</span>
                    <span class="c1"># parts. This makes the result of parsing e.g.</span>
                    <span class="c1"># (&quot;C:&quot;, &quot;/&quot;, &quot;a&quot;) reasonably intuitive.</span>
                    <span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
                        <span class="k">if</span> <span class="ow">not</span> <span class="n">part</span><span class="p">:</span>
                            <span class="k">continue</span>
                        <span class="k">if</span> <span class="n">altsep</span><span class="p">:</span>
                            <span class="n">part</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">altsep</span><span class="p">,</span> <span class="n">sep</span><span class="p">)</span>
                        <span class="n">drv</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">splitroot</span><span class="p">(</span><span class="n">part</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
                        <span class="k">if</span> <span class="n">drv</span><span class="p">:</span>
                            <span class="k">break</span>
                <span class="k">break</span>
        <span class="k">if</span> <span class="n">drv</span> <span class="ow">or</span> <span class="n">root</span><span class="p">:</span>
            <span class="n">parsed</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">drv</span> <span class="o">+</span> <span class="n">root</span><span class="p">)</span>
        <span class="n">parsed</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parsed</span>

    <span class="k">def</span> <span class="nf">join_parsed_parts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span><span class="p">,</span> <span class="n">drv2</span><span class="p">,</span> <span class="n">root2</span><span class="p">,</span> <span class="n">parts2</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Join the two paths represented by the respective</span>
<span class="sd">        (drive, root, parts) tuples.  Return a new (drive, root, parts) tuple.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">root2</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">drv2</span> <span class="ow">and</span> <span class="n">drv</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">drv</span><span class="p">,</span> <span class="n">root2</span><span class="p">,</span> <span class="p">[</span><span class="n">drv</span> <span class="o">+</span> <span class="n">root2</span><span class="p">]</span> <span class="o">+</span> <span class="n">parts2</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">elif</span> <span class="n">drv2</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">drv2</span> <span class="o">==</span> <span class="n">drv</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">casefold</span><span class="p">(</span><span class="n">drv2</span><span class="p">)</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">casefold</span><span class="p">(</span><span class="n">drv</span><span class="p">):</span>
                <span class="c1"># Same drive =&gt; second path is relative to the first</span>
                <span class="k">return</span> <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span> <span class="o">+</span> <span class="n">parts2</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Second path is non-anchored (common case)</span>
            <span class="k">return</span> <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span> <span class="o">+</span> <span class="n">parts2</span>
        <span class="k">return</span> <span class="n">drv2</span><span class="p">,</span> <span class="n">root2</span><span class="p">,</span> <span class="n">parts2</span>


<span class="k">class</span> <span class="nc">_WindowsFlavour</span><span class="p">(</span><span class="n">_Flavour</span><span class="p">):</span>
    <span class="c1"># Reference for Windows paths can be found at</span>
    <span class="c1"># http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx</span>

    <span class="n">sep</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span>
    <span class="n">altsep</span> <span class="o">=</span> <span class="s1">&#39;/&#39;</span>
    <span class="n">has_drv</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">pathmod</span> <span class="o">=</span> <span class="n">ntpath</span>

    <span class="n">is_supported</span> <span class="o">=</span> <span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;nt&#39;</span><span class="p">)</span>

    <span class="n">drive_letters</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="s1">&#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;</span><span class="p">)</span>
    <span class="n">ext_namespace_prefix</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\\\\</span><span class="s1">?</span><span class="se">\\</span><span class="s1">&#39;</span>

    <span class="n">reserved_names</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">{</span><span class="s1">&#39;CON&#39;</span><span class="p">,</span> <span class="s1">&#39;PRN&#39;</span><span class="p">,</span> <span class="s1">&#39;AUX&#39;</span><span class="p">,</span> <span class="s1">&#39;NUL&#39;</span><span class="p">}</span> <span class="o">|</span>
        <span class="p">{</span><span class="s1">&#39;COM</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">)}</span> <span class="o">|</span>
        <span class="p">{</span><span class="s1">&#39;LPT</span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">)}</span>
        <span class="p">)</span>

    <span class="c1"># Interesting findings about extended paths:</span>
    <span class="c1"># - &#39;\\?\c:\a&#39;, &#39;//?/c:\a&#39; and &#39;//?/c:/a&#39; are all supported</span>
    <span class="c1">#   but &#39;\\?\c:/a&#39; is not</span>
    <span class="c1"># - extended paths are always absolute; &quot;relative&quot; extended paths will</span>
    <span class="c1">#   fail.</span>

    <span class="k">def</span> <span class="nf">splitroot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">part</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="n">sep</span><span class="p">):</span>
        <span class="n">first</span> <span class="o">=</span> <span class="n">part</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">second</span> <span class="o">=</span> <span class="n">part</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">second</span> <span class="o">==</span> <span class="n">sep</span> <span class="ow">and</span> <span class="n">first</span> <span class="o">==</span> <span class="n">sep</span><span class="p">):</span>
            <span class="c1"># XXX extended paths should also disable the collapsing of &quot;.&quot;</span>
            <span class="c1"># components (according to MSDN docs).</span>
            <span class="n">prefix</span><span class="p">,</span> <span class="n">part</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_split_extended_path</span><span class="p">(</span><span class="n">part</span><span class="p">)</span>
            <span class="n">first</span> <span class="o">=</span> <span class="n">part</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">second</span> <span class="o">=</span> <span class="n">part</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">prefix</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="n">third</span> <span class="o">=</span> <span class="n">part</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">second</span> <span class="o">==</span> <span class="n">sep</span> <span class="ow">and</span> <span class="n">first</span> <span class="o">==</span> <span class="n">sep</span> <span class="ow">and</span> <span class="n">third</span> <span class="o">!=</span> <span class="n">sep</span><span class="p">):</span>
            <span class="c1"># is a UNC path:</span>
            <span class="c1"># vvvvvvvvvvvvvvvvvvvvv root</span>
            <span class="c1"># \\machine\mountpoint\directory\etc\...</span>
            <span class="c1">#            directory ^^^^^^^^^^^^^^</span>
            <span class="n">index</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">sep</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">index</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="n">index2</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">sep</span><span class="p">,</span> <span class="n">index</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
                <span class="c1"># a UNC path can&#39;t have two slashes in a row</span>
                <span class="c1"># (after the initial two)</span>
                <span class="k">if</span> <span class="n">index2</span> <span class="o">!=</span> <span class="n">index</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">index2</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                        <span class="n">index2</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">part</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">prefix</span><span class="p">:</span>
                        <span class="k">return</span> <span class="n">prefix</span> <span class="o">+</span> <span class="n">part</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="n">index2</span><span class="p">],</span> <span class="n">sep</span><span class="p">,</span> <span class="n">part</span><span class="p">[</span><span class="n">index2</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">return</span> <span class="n">part</span><span class="p">[:</span><span class="n">index2</span><span class="p">],</span> <span class="n">sep</span><span class="p">,</span> <span class="n">part</span><span class="p">[</span><span class="n">index2</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
        <span class="n">drv</span> <span class="o">=</span> <span class="n">root</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">if</span> <span class="n">second</span> <span class="o">==</span> <span class="s1">&#39;:&#39;</span> <span class="ow">and</span> <span class="n">first</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">drive_letters</span><span class="p">:</span>
            <span class="n">drv</span> <span class="o">=</span> <span class="n">part</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span>
            <span class="n">part</span> <span class="o">=</span> <span class="n">part</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span>
            <span class="n">first</span> <span class="o">=</span> <span class="n">third</span>
        <span class="k">if</span> <span class="n">first</span> <span class="o">==</span> <span class="n">sep</span><span class="p">:</span>
            <span class="n">root</span> <span class="o">=</span> <span class="n">first</span>
            <span class="n">part</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="n">sep</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">prefix</span> <span class="o">+</span> <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">part</span>

    <span class="k">def</span> <span class="nf">casefold</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">s</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">casefold_parts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parts</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">p</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">parts</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">resolve</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="n">s</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">s</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span>
        <span class="n">previous_s</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">_getfinalpathname</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">strict</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ext_to_normal</span><span class="p">(</span><span class="n">_getfinalpathname</span><span class="p">(</span><span class="n">s</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">tail_parts</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># End of the path after the first one not found</span>
                <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">s</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ext_to_normal</span><span class="p">(</span><span class="n">_getfinalpathname</span><span class="p">(</span><span class="n">s</span><span class="p">))</span>
                    <span class="k">except</span> <span class="ne">FileNotFoundError</span><span class="p">:</span>
                        <span class="n">previous_s</span> <span class="o">=</span> <span class="n">s</span>
                        <span class="n">s</span><span class="p">,</span> <span class="n">tail</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
                        <span class="n">tail_parts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tail</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">previous_s</span> <span class="o">==</span> <span class="n">s</span><span class="p">:</span>
                            <span class="k">return</span> <span class="n">path</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="o">*</span><span class="nb">reversed</span><span class="p">(</span><span class="n">tail_parts</span><span class="p">))</span>
        <span class="c1"># Means fallback on absolute</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">_split_extended_path</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">ext_prefix</span><span class="o">=</span><span class="n">ext_namespace_prefix</span><span class="p">):</span>
        <span class="n">prefix</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">ext_prefix</span><span class="p">):</span>
            <span class="n">prefix</span> <span class="o">=</span> <span class="n">s</span><span class="p">[:</span><span class="mi">4</span><span class="p">]</span>
            <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="p">[</span><span class="mi">4</span><span class="p">:]</span>
            <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;UNC</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
                <span class="n">prefix</span> <span class="o">+=</span> <span class="n">s</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span>
                <span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="n">s</span><span class="p">[</span><span class="mi">3</span><span class="p">:]</span>
        <span class="k">return</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">s</span>

    <span class="k">def</span> <span class="nf">_ext_to_normal</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">):</span>
        <span class="c1"># Turn back an extended path into a normal DOS-like path</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_split_extended_path</span><span class="p">(</span><span class="n">s</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">is_reserved</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parts</span><span class="p">):</span>
        <span class="c1"># NOTE: the rules for reserved names seem somewhat complicated</span>
        <span class="c1"># (e.g. r&quot;..\NUL&quot; is reserved but not r&quot;foo\NUL&quot;).</span>
        <span class="c1"># We err on the side of caution and return True for paths which are</span>
        <span class="c1"># not considered reserved by Windows.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">parts</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="n">parts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\\\</span><span class="s1">&#39;</span><span class="p">):</span>
            <span class="c1"># UNC paths are never reserved</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="n">parts</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">partition</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">reserved_names</span>

    <span class="k">def</span> <span class="nf">make_uri</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="c1"># Under Windows, file URIs use the UTF-8 encoding.</span>
        <span class="n">drive</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">drive</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">drive</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span> <span class="ow">and</span> <span class="n">drive</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;:&#39;</span><span class="p">:</span>
            <span class="c1"># It&#39;s a path on a local drive =&gt; &#39;file:///c:/a/b&#39;</span>
            <span class="n">rest</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">as_posix</span><span class="p">()[</span><span class="mi">2</span><span class="p">:]</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="s1">&#39;file:///</span><span class="si">%s</span><span class="s1">/</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">drive</span><span class="p">,</span> <span class="n">urlquote_from_bytes</span><span class="p">(</span><span class="n">rest</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># It&#39;s a path on a network drive =&gt; &#39;file://host/share/a/b&#39;</span>
            <span class="k">return</span> <span class="s1">&#39;file:&#39;</span> <span class="o">+</span> <span class="n">urlquote_from_bytes</span><span class="p">(</span><span class="n">path</span><span class="o">.</span><span class="n">as_posix</span><span class="p">()</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">gethomedir</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
        <span class="k">if</span> <span class="s1">&#39;HOME&#39;</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">:</span>
            <span class="n">userhome</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;HOME&#39;</span><span class="p">]</span>
        <span class="k">elif</span> <span class="s1">&#39;USERPROFILE&#39;</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">:</span>
            <span class="n">userhome</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;USERPROFILE&#39;</span><span class="p">]</span>
        <span class="k">elif</span> <span class="s1">&#39;HOMEPATH&#39;</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">drv</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;HOMEDRIVE&#39;</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="n">drv</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
            <span class="n">userhome</span> <span class="o">=</span> <span class="n">drv</span> <span class="o">+</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;HOMEPATH&#39;</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Can&#39;t determine home directory&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">username</span><span class="p">:</span>
            <span class="c1"># Try to guess user home directory.  By default all users</span>
            <span class="c1"># directories are located in the same place and are named by</span>
            <span class="c1"># corresponding usernames.  If current user home directory points</span>
            <span class="c1"># to nonstandard place, this guess is likely wrong.</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;USERNAME&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="n">username</span><span class="p">:</span>
                <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parse_parts</span><span class="p">((</span><span class="n">userhome</span><span class="p">,))</span>
                <span class="k">if</span> <span class="n">parts</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">!=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;USERNAME&#39;</span><span class="p">]:</span>
                    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Can&#39;t determine home directory &quot;</span>
                                       <span class="s2">&quot;for </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">username</span><span class="p">)</span>
                <span class="n">parts</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">username</span>
                <span class="k">if</span> <span class="n">drv</span> <span class="ow">or</span> <span class="n">root</span><span class="p">:</span>
                    <span class="n">userhome</span> <span class="o">=</span> <span class="n">drv</span> <span class="o">+</span> <span class="n">root</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">parts</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">userhome</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">parts</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">userhome</span>

<span class="k">class</span> <span class="nc">_PosixFlavour</span><span class="p">(</span><span class="n">_Flavour</span><span class="p">):</span>
    <span class="n">sep</span> <span class="o">=</span> <span class="s1">&#39;/&#39;</span>
    <span class="n">altsep</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="n">has_drv</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">pathmod</span> <span class="o">=</span> <span class="n">posixpath</span>

    <span class="n">is_supported</span> <span class="o">=</span> <span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">name</span> <span class="o">!=</span> <span class="s1">&#39;nt&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">splitroot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">part</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="n">sep</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">part</span> <span class="ow">and</span> <span class="n">part</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">sep</span><span class="p">:</span>
            <span class="n">stripped_part</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="n">sep</span><span class="p">)</span>
            <span class="c1"># According to POSIX path resolution:</span>
            <span class="c1"># http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11</span>
            <span class="c1"># &quot;A pathname that begins with two successive slashes may be</span>
            <span class="c1"># interpreted in an implementation-defined manner, although more</span>
            <span class="c1"># than two leading slashes shall be treated as a single slash&quot;.</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">part</span><span class="p">)</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">stripped_part</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                <span class="k">return</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">sep</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">stripped_part</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">sep</span><span class="p">,</span> <span class="n">stripped_part</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">part</span>

    <span class="k">def</span> <span class="nf">casefold</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">s</span>

    <span class="k">def</span> <span class="nf">casefold_parts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parts</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">parts</span>

    <span class="k">def</span> <span class="nf">resolve</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="n">sep</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sep</span>
        <span class="n">accessor</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">_accessor</span>
        <span class="n">seen</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">def</span> <span class="nf">_resolve</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">rest</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">rest</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">sep</span><span class="p">):</span>
                <span class="n">path</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

            <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">rest</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">sep</span><span class="p">):</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span> <span class="ow">or</span> <span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;.&#39;</span><span class="p">:</span>
                    <span class="c1"># current dir</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;..&#39;</span><span class="p">:</span>
                    <span class="c1"># parent dir</span>
                    <span class="n">path</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">rpartition</span><span class="p">(</span><span class="n">sep</span><span class="p">)</span>
                    <span class="k">continue</span>
                <span class="n">newpath</span> <span class="o">=</span> <span class="n">path</span> <span class="o">+</span> <span class="n">sep</span> <span class="o">+</span> <span class="n">name</span>
                <span class="k">if</span> <span class="n">newpath</span> <span class="ow">in</span> <span class="n">seen</span><span class="p">:</span>
                    <span class="c1"># Already seen this path</span>
                    <span class="n">path</span> <span class="o">=</span> <span class="n">seen</span><span class="p">[</span><span class="n">newpath</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">path</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="c1"># use cached value</span>
                        <span class="k">continue</span>
                    <span class="c1"># The symlink is not resolved, so we must have a symlink loop.</span>
                    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Symlink loop from </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">newpath</span><span class="p">)</span>
                <span class="c1"># Resolve the symbolic link</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">target</span> <span class="o">=</span> <span class="n">accessor</span><span class="o">.</span><span class="n">readlink</span><span class="p">(</span><span class="n">newpath</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">errno</span> <span class="o">!=</span> <span class="n">EINVAL</span> <span class="ow">and</span> <span class="n">strict</span><span class="p">:</span>
                        <span class="k">raise</span>
                    <span class="c1"># Not a symlink, or non-strict mode. We just leave the path</span>
                    <span class="c1"># untouched.</span>
                    <span class="n">path</span> <span class="o">=</span> <span class="n">newpath</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">seen</span><span class="p">[</span><span class="n">newpath</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># not resolved symlink</span>
                    <span class="n">path</span> <span class="o">=</span> <span class="n">_resolve</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">target</span><span class="p">)</span>
                    <span class="n">seen</span><span class="p">[</span><span class="n">newpath</span><span class="p">]</span> <span class="o">=</span> <span class="n">path</span> <span class="c1"># resolved symlink</span>

            <span class="k">return</span> <span class="n">path</span>
        <span class="c1"># NOTE: according to POSIX, getcwd() cannot contain path components</span>
        <span class="c1"># which are symlinks.</span>
        <span class="n">base</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span> <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">is_absolute</span><span class="p">()</span> <span class="k">else</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">_resolve</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">path</span><span class="p">))</span> <span class="ow">or</span> <span class="n">sep</span>

    <span class="k">def</span> <span class="nf">is_reserved</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parts</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">make_uri</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="c1"># We represent the path using the local filesystem encoding,</span>
        <span class="c1"># for portability to other applications.</span>
        <span class="n">bpath</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
        <span class="k">return</span> <span class="s1">&#39;file://&#39;</span> <span class="o">+</span> <span class="n">urlquote_from_bytes</span><span class="p">(</span><span class="n">bpath</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">gethomedir</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">username</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;HOME&#39;</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="kn">import</span> <span class="nn">pwd</span>
                <span class="k">return</span> <span class="n">pwd</span><span class="o">.</span><span class="n">getpwuid</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getuid</span><span class="p">())</span><span class="o">.</span><span class="n">pw_dir</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="kn">import</span> <span class="nn">pwd</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">pwd</span><span class="o">.</span><span class="n">getpwnam</span><span class="p">(</span><span class="n">username</span><span class="p">)</span><span class="o">.</span><span class="n">pw_dir</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Can&#39;t determine home directory &quot;</span>
                                   <span class="s2">&quot;for </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">username</span><span class="p">)</span>


<span class="n">_windows_flavour</span> <span class="o">=</span> <span class="n">_WindowsFlavour</span><span class="p">()</span>
<span class="n">_posix_flavour</span> <span class="o">=</span> <span class="n">_PosixFlavour</span><span class="p">()</span>


<span class="k">class</span> <span class="nc">_Accessor</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;An accessor implements a particular (system-specific or not) way of</span>
<span class="sd">    accessing paths on the filesystem.&quot;&quot;&quot;</span>


<span class="k">class</span> <span class="nc">_NormalAccessor</span><span class="p">(</span><span class="n">_Accessor</span><span class="p">):</span>

    <span class="n">stat</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">stat</span>

    <span class="n">lstat</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">lstat</span>

    <span class="nb">open</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">open</span>

    <span class="n">listdir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span>

    <span class="n">scandir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">scandir</span>

    <span class="n">chmod</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">chmod</span>

    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">os</span><span class="p">,</span> <span class="s2">&quot;lchmod&quot;</span><span class="p">):</span>
        <span class="n">lchmod</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">lchmod</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">def</span> <span class="nf">lchmod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pathobj</span><span class="p">,</span> <span class="n">mode</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;lchmod() not available on this system&quot;</span><span class="p">)</span>

    <span class="n">mkdir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">mkdir</span>

    <span class="n">unlink</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">unlink</span>

    <span class="n">rmdir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">rmdir</span>

    <span class="n">rename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">rename</span>

    <span class="n">replace</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">replace</span>

    <span class="k">if</span> <span class="n">nt</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">supports_symlinks</span><span class="p">:</span>
            <span class="n">symlink</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">symlink</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">def</span> <span class="nf">symlink</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">target_is_directory</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;symlink() not available on this system&quot;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># Under POSIX, os.symlink() takes two args</span>
        <span class="nd">@staticmethod</span>
        <span class="k">def</span> <span class="nf">symlink</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">target_is_directory</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">symlink</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>

    <span class="n">utime</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">utime</span>

    <span class="c1"># Helper for resolve()</span>
    <span class="k">def</span> <span class="nf">readlink</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">readlink</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>


<span class="n">_normal_accessor</span> <span class="o">=</span> <span class="n">_NormalAccessor</span><span class="p">()</span>


<span class="c1">#</span>
<span class="c1"># Globbing helpers</span>
<span class="c1">#</span>

<span class="k">def</span> <span class="nf">_make_selector</span><span class="p">(</span><span class="n">pattern_parts</span><span class="p">):</span>
    <span class="n">pat</span> <span class="o">=</span> <span class="n">pattern_parts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">child_parts</span> <span class="o">=</span> <span class="n">pattern_parts</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
    <span class="k">if</span> <span class="n">pat</span> <span class="o">==</span> <span class="s1">&#39;**&#39;</span><span class="p">:</span>
        <span class="bp">cls</span> <span class="o">=</span> <span class="n">_RecursiveWildcardSelector</span>
    <span class="k">elif</span> <span class="s1">&#39;**&#39;</span> <span class="ow">in</span> <span class="n">pat</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Invalid pattern: &#39;**&#39; can only be an entire path component&quot;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">_is_wildcard_pattern</span><span class="p">(</span><span class="n">pat</span><span class="p">):</span>
        <span class="bp">cls</span> <span class="o">=</span> <span class="n">_WildcardSelector</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="bp">cls</span> <span class="o">=</span> <span class="n">_PreciseSelector</span>
    <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">pat</span><span class="p">,</span> <span class="n">child_parts</span><span class="p">)</span>

<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">functools</span><span class="p">,</span> <span class="s2">&quot;lru_cache&quot;</span><span class="p">):</span>
    <span class="n">_make_selector</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">lru_cache</span><span class="p">()(</span><span class="n">_make_selector</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_Selector</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;A selector matches a specific glob pattern part against the children</span>
<span class="sd">    of a given path.&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">child_parts</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">child_parts</span> <span class="o">=</span> <span class="n">child_parts</span>
        <span class="k">if</span> <span class="n">child_parts</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">successor</span> <span class="o">=</span> <span class="n">_make_selector</span><span class="p">(</span><span class="n">child_parts</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dironly</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">successor</span> <span class="o">=</span> <span class="n">_TerminatingSelector</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dironly</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">select_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent_path</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Iterate over all child paths of `parent_path` matched by this</span>
<span class="sd">        selector.  This can contain parent_path itself.&quot;&quot;&quot;</span>
        <span class="n">path_cls</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">parent_path</span><span class="p">)</span>
        <span class="n">is_dir</span> <span class="o">=</span> <span class="n">path_cls</span><span class="o">.</span><span class="n">is_dir</span>
        <span class="n">exists</span> <span class="o">=</span> <span class="n">path_cls</span><span class="o">.</span><span class="n">exists</span>
        <span class="n">scandir</span> <span class="o">=</span> <span class="n">parent_path</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">scandir</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">is_dir</span><span class="p">(</span><span class="n">parent_path</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">iter</span><span class="p">([])</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_select_from</span><span class="p">(</span><span class="n">parent_path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">,</span> <span class="n">exists</span><span class="p">,</span> <span class="n">scandir</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_TerminatingSelector</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">_select_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent_path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">,</span> <span class="n">exists</span><span class="p">,</span> <span class="n">scandir</span><span class="p">):</span>
        <span class="k">yield</span> <span class="n">parent_path</span>


<span class="k">class</span> <span class="nc">_PreciseSelector</span><span class="p">(</span><span class="n">_Selector</span><span class="p">):</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">child_parts</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="n">_Selector</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">child_parts</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_select_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent_path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">,</span> <span class="n">exists</span><span class="p">,</span> <span class="n">scandir</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">parent_path</span><span class="o">.</span><span class="n">_make_child_relpath</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">is_dir</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dironly</span> <span class="k">else</span> <span class="n">exists</span><span class="p">)(</span><span class="n">path</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">successor</span><span class="o">.</span><span class="n">_select_from</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">,</span> <span class="n">exists</span><span class="p">,</span> <span class="n">scandir</span><span class="p">):</span>
                    <span class="k">yield</span> <span class="n">p</span>
        <span class="k">except</span> <span class="ne">PermissionError</span><span class="p">:</span>
            <span class="k">return</span>


<span class="k">class</span> <span class="nc">_WildcardSelector</span><span class="p">(</span><span class="n">_Selector</span><span class="p">):</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pat</span><span class="p">,</span> <span class="n">child_parts</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pat</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">fnmatch</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">pat</span><span class="p">))</span>
        <span class="n">_Selector</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">child_parts</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_select_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent_path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">,</span> <span class="n">exists</span><span class="p">,</span> <span class="n">scandir</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">cf</span> <span class="o">=</span> <span class="n">parent_path</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">casefold</span>
            <span class="n">entries</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">scandir</span><span class="p">(</span><span class="n">parent_path</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">entries</span><span class="p">:</span>
                <span class="n">entry_is_dir</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">entry_is_dir</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">is_dir</span><span class="p">()</span>
                <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">_ignore_error</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
                        <span class="k">raise</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">dironly</span> <span class="ow">or</span> <span class="n">entry_is_dir</span><span class="p">:</span>
                    <span class="n">name</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">name</span>
                    <span class="n">casefolded</span> <span class="o">=</span> <span class="n">cf</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pat</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">casefolded</span><span class="p">):</span>
                        <span class="n">path</span> <span class="o">=</span> <span class="n">parent_path</span><span class="o">.</span><span class="n">_make_child_relpath</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
                        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">successor</span><span class="o">.</span><span class="n">_select_from</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">,</span> <span class="n">exists</span><span class="p">,</span> <span class="n">scandir</span><span class="p">):</span>
                            <span class="k">yield</span> <span class="n">p</span>
        <span class="k">except</span> <span class="ne">PermissionError</span><span class="p">:</span>
            <span class="k">return</span>



<span class="k">class</span> <span class="nc">_RecursiveWildcardSelector</span><span class="p">(</span><span class="n">_Selector</span><span class="p">):</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pat</span><span class="p">,</span> <span class="n">child_parts</span><span class="p">):</span>
        <span class="n">_Selector</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">child_parts</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_iterate_directories</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent_path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">,</span> <span class="n">scandir</span><span class="p">):</span>
        <span class="k">yield</span> <span class="n">parent_path</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">entries</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">scandir</span><span class="p">(</span><span class="n">parent_path</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">entries</span><span class="p">:</span>
                <span class="n">entry_is_dir</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">entry_is_dir</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">is_dir</span><span class="p">()</span>
                <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">_ignore_error</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
                        <span class="k">raise</span>
                <span class="k">if</span> <span class="n">entry_is_dir</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">entry</span><span class="o">.</span><span class="n">is_symlink</span><span class="p">():</span>
                    <span class="n">path</span> <span class="o">=</span> <span class="n">parent_path</span><span class="o">.</span><span class="n">_make_child_relpath</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_iterate_directories</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">,</span> <span class="n">scandir</span><span class="p">):</span>
                        <span class="k">yield</span> <span class="n">p</span>
        <span class="k">except</span> <span class="ne">PermissionError</span><span class="p">:</span>
            <span class="k">return</span>

    <span class="k">def</span> <span class="nf">_select_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent_path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">,</span> <span class="n">exists</span><span class="p">,</span> <span class="n">scandir</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">yielded</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">successor_select</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">successor</span><span class="o">.</span><span class="n">_select_from</span>
                <span class="k">for</span> <span class="n">starting_point</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_iterate_directories</span><span class="p">(</span><span class="n">parent_path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">,</span> <span class="n">scandir</span><span class="p">):</span>
                    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">successor_select</span><span class="p">(</span><span class="n">starting_point</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">,</span> <span class="n">exists</span><span class="p">,</span> <span class="n">scandir</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">p</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">yielded</span><span class="p">:</span>
                            <span class="k">yield</span> <span class="n">p</span>
                            <span class="n">yielded</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
            <span class="k">finally</span><span class="p">:</span>
                <span class="n">yielded</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">PermissionError</span><span class="p">:</span>
            <span class="k">return</span>


<span class="c1">#</span>
<span class="c1"># Public API</span>
<span class="c1">#</span>

<span class="k">class</span> <span class="nc">_PathParents</span><span class="p">(</span><span class="n">Sequence</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;This object provides sequence-like access to the logical ancestors</span>
<span class="sd">    of a path.  Don&#39;t try to construct it yourself.&quot;&quot;&quot;</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;_pathcls&#39;</span><span class="p">,</span> <span class="s1">&#39;_drv&#39;</span><span class="p">,</span> <span class="s1">&#39;_root&#39;</span><span class="p">,</span> <span class="s1">&#39;_parts&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="c1"># We don&#39;t store the instance to avoid reference cycles</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_pathcls</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_drv</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">_drv</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_root</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">_root</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">_parts</span>

    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_drv</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">idx</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">idx</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">idx</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pathcls</span><span class="o">.</span><span class="n">_from_parsed_parts</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_drv</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">,</span>
                                                <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">[:</span><span class="o">-</span><span class="n">idx</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;&lt;</span><span class="si">{}</span><span class="s2">.parents&gt;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_pathcls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">PurePath</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Base class for manipulating paths without I/O.</span>

<span class="sd">    PurePath represents a filesystem path and offers operations which</span>
<span class="sd">    don&#39;t imply any actual filesystem I/O.  Depending on your system,</span>
<span class="sd">    instantiating a PurePath will return either a PurePosixPath or a</span>
<span class="sd">    PureWindowsPath object.  You can also instantiate either of these classes</span>
<span class="sd">    directly, regardless of your system.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span>
        <span class="s1">&#39;_drv&#39;</span><span class="p">,</span> <span class="s1">&#39;_root&#39;</span><span class="p">,</span> <span class="s1">&#39;_parts&#39;</span><span class="p">,</span>
        <span class="s1">&#39;_str&#39;</span><span class="p">,</span> <span class="s1">&#39;_hash&#39;</span><span class="p">,</span> <span class="s1">&#39;_pparts&#39;</span><span class="p">,</span> <span class="s1">&#39;_cached_cparts&#39;</span><span class="p">,</span>
    <span class="p">)</span>

    <span class="k">def</span> <span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Construct a PurePath from one or several strings and or existing</span>
<span class="sd">        PurePath objects.  The strings and path objects are combined so as</span>
<span class="sd">        to yield a canonicalized path, which is incorporated into the</span>
<span class="sd">        new PurePath object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">cls</span> <span class="ow">is</span> <span class="n">PurePath</span><span class="p">:</span>
            <span class="bp">cls</span> <span class="o">=</span> <span class="n">PureWindowsPath</span> <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;nt&#39;</span> <span class="k">else</span> <span class="n">PurePosixPath</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_from_parts</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__reduce__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Using the parts tuple helps share interned path parts</span>
        <span class="c1"># when pickling related paths.</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">))</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_parse_args</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
        <span class="c1"># This is useful when you don&#39;t want to create an instance, just</span>
        <span class="c1"># canonicalize some constructor arguments.</span>
        <span class="n">parts</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">PurePath</span><span class="p">):</span>
                <span class="n">parts</span> <span class="o">+=</span> <span class="n">a</span><span class="o">.</span><span class="n">_parts</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">a</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fspath</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                    <span class="c1"># Force-cast str subclasses to str (issue #21127)</span>
                    <span class="n">parts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                        <span class="s2">&quot;argument should be a str object or an os.PathLike &quot;</span>
                        <span class="s2">&quot;object returning str, not </span><span class="si">%r</span><span class="s2">&quot;</span>
                        <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">parse_parts</span><span class="p">(</span><span class="n">parts</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_from_parts</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">init</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="c1"># We need to call _parse_args on the instance, so as to get the</span>
        <span class="c1"># right flavour.</span>
        <span class="bp">self</span> <span class="o">=</span> <span class="nb">object</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>
        <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parse_args</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_drv</span> <span class="o">=</span> <span class="n">drv</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_root</span> <span class="o">=</span> <span class="n">root</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span> <span class="o">=</span> <span class="n">parts</span>
        <span class="k">if</span> <span class="n">init</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_init</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_from_parsed_parts</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span><span class="p">,</span> <span class="n">init</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="bp">self</span> <span class="o">=</span> <span class="nb">object</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_drv</span> <span class="o">=</span> <span class="n">drv</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_root</span> <span class="o">=</span> <span class="n">root</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span> <span class="o">=</span> <span class="n">parts</span>
        <span class="k">if</span> <span class="n">init</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_init</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_format_parsed_parts</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">drv</span> <span class="ow">or</span> <span class="n">root</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">drv</span> <span class="o">+</span> <span class="n">root</span> <span class="o">+</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">parts</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">parts</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_init</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Overridden in concrete Path</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">_make_child</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
        <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parse_args</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
        <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">join_parsed_parts</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_drv</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">,</span> <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_from_parsed_parts</span><span class="p">(</span><span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the string representation of the path, suitable for</span>
<span class="sd">        passing to system calls.&quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_str</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_str</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_format_parsed_parts</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_drv</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">,</span>
                                                  <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">)</span> <span class="ow">or</span> <span class="s1">&#39;.&#39;</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_str</span>

    <span class="k">def</span> <span class="nf">__fspath__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">as_posix</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the string representation of the path with forward (/)</span>
<span class="sd">        slashes.&quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">sep</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__bytes__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the bytes representation of the path.  This is only</span>
<span class="sd">        recommended to use under Unix.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">fsencode</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">(</span><span class="si">{!r}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">as_posix</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">as_uri</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the path as a &#39;file&#39; URI.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_absolute</span><span class="p">():</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;relative path can&#39;t be expressed as a file URI&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">make_uri</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">_cparts</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Cached casefolded parts, for hashing and comparison</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cached_cparts</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cached_cparts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">casefold_parts</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">)</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cached_cparts</span>

    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">PurePath</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cparts</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">_cparts</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span> <span class="ow">is</span> <span class="n">other</span><span class="o">.</span><span class="n">_flavour</span>

    <span class="k">def</span> <span class="fm">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_hash</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_hash</span> <span class="o">=</span> <span class="nb">hash</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_cparts</span><span class="p">))</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_hash</span>

    <span class="k">def</span> <span class="fm">__lt__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">PurePath</span><span class="p">)</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">other</span><span class="o">.</span><span class="n">_flavour</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cparts</span> <span class="o">&lt;</span> <span class="n">other</span><span class="o">.</span><span class="n">_cparts</span>

    <span class="k">def</span> <span class="fm">__le__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">PurePath</span><span class="p">)</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">other</span><span class="o">.</span><span class="n">_flavour</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cparts</span> <span class="o">&lt;=</span> <span class="n">other</span><span class="o">.</span><span class="n">_cparts</span>

    <span class="k">def</span> <span class="fm">__gt__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">PurePath</span><span class="p">)</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">other</span><span class="o">.</span><span class="n">_flavour</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cparts</span> <span class="o">&gt;</span> <span class="n">other</span><span class="o">.</span><span class="n">_cparts</span>

    <span class="k">def</span> <span class="fm">__ge__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">PurePath</span><span class="p">)</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">other</span><span class="o">.</span><span class="n">_flavour</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">NotImplemented</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cparts</span> <span class="o">&gt;=</span> <span class="n">other</span><span class="o">.</span><span class="n">_cparts</span>

    <span class="n">drive</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">attrgetter</span><span class="p">(</span><span class="s1">&#39;_drv&#39;</span><span class="p">),</span>
                     <span class="n">doc</span><span class="o">=</span><span class="s2">&quot;&quot;&quot;The drive prefix (letter or UNC path), if any.&quot;&quot;&quot;</span><span class="p">)</span>

    <span class="n">root</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">attrgetter</span><span class="p">(</span><span class="s1">&#39;_root&#39;</span><span class="p">),</span>
                    <span class="n">doc</span><span class="o">=</span><span class="s2">&quot;&quot;&quot;The root of the path, if any.&quot;&quot;&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">anchor</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;The concatenation of the drive and root, or &#39;&#39;.&quot;&quot;&quot;</span>
        <span class="n">anchor</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_drv</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span>
        <span class="k">return</span> <span class="n">anchor</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;The final path component, if any.&quot;&quot;&quot;</span>
        <span class="n">parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">parts</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="mi">1</span> <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_drv</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">)</span> <span class="k">else</span> <span class="mi">0</span><span class="p">):</span>
            <span class="k">return</span> <span class="s1">&#39;&#39;</span>
        <span class="k">return</span> <span class="n">parts</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">suffix</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;The final component&#39;s last suffix, if any.&quot;&quot;&quot;</span>
        <span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="n">i</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="mi">0</span> <span class="o">&lt;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">name</span><span class="p">[</span><span class="n">i</span><span class="p">:]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;&#39;</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">suffixes</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;A list of the final component&#39;s suffixes, if any.&quot;&quot;&quot;</span>
        <span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="k">if</span> <span class="n">name</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">[</span><span class="s1">&#39;.&#39;</span> <span class="o">+</span> <span class="n">suffix</span> <span class="k">for</span> <span class="n">suffix</span> <span class="ow">in</span> <span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)[</span><span class="mi">1</span><span class="p">:]]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">stem</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;The final path component, minus its last suffix.&quot;&quot;&quot;</span>
        <span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="n">i</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="mi">0</span> <span class="o">&lt;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">name</span><span class="p">[:</span><span class="n">i</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">name</span>

    <span class="k">def</span> <span class="nf">with_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a new path with the file name changed.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%r</span><span class="s2"> has an empty name&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,))</span>
        <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">parse_parts</span><span class="p">((</span><span class="n">name</span><span class="p">,))</span>
        <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">name</span> <span class="ow">or</span> <span class="n">name</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="ow">in</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">sep</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">altsep</span><span class="p">]</span>
            <span class="ow">or</span> <span class="n">drv</span> <span class="ow">or</span> <span class="n">root</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">parts</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Invalid name </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">))</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_from_parsed_parts</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_drv</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">,</span>
                                       <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="n">name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">with_suffix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">suffix</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a new path with the file suffix changed.  If the path</span>
<span class="sd">        has no suffix, add given suffix.  If the given suffix is an empty</span>
<span class="sd">        string, remove the suffix from the path.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span>
        <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">sep</span> <span class="ow">in</span> <span class="n">suffix</span> <span class="ow">or</span> <span class="n">f</span><span class="o">.</span><span class="n">altsep</span> <span class="ow">and</span> <span class="n">f</span><span class="o">.</span><span class="n">altsep</span> <span class="ow">in</span> <span class="n">suffix</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Invalid suffix </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">suffix</span><span class="p">,))</span>
        <span class="k">if</span> <span class="n">suffix</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">suffix</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">suffix</span> <span class="o">==</span> <span class="s1">&#39;.&#39;</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Invalid suffix </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">suffix</span><span class="p">))</span>
        <span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%r</span><span class="s2"> has an empty name&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,))</span>
        <span class="n">old_suffix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">suffix</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">old_suffix</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">name</span> <span class="o">+</span> <span class="n">suffix</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="p">[:</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">old_suffix</span><span class="p">)]</span> <span class="o">+</span> <span class="n">suffix</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_from_parsed_parts</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_drv</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">,</span>
                                       <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="n">name</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">relative_to</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">other</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the relative path to another path identified by the passed</span>
<span class="sd">        arguments.  If the operation is not possible (because this is not</span>
<span class="sd">        a subpath of the other path), raise ValueError.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># For the purpose of this method, drive and root are considered</span>
        <span class="c1"># separate parts, i.e.:</span>
        <span class="c1">#   Path(&#39;c:/&#39;).relative_to(&#39;c:&#39;)  gives Path(&#39;/&#39;)</span>
        <span class="c1">#   Path(&#39;c:/&#39;).relative_to(&#39;/&#39;)   raise ValueError</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">other</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;need at least one argument&quot;</span><span class="p">)</span>
        <span class="n">parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span>
        <span class="n">drv</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_drv</span>
        <span class="n">root</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span>
        <span class="k">if</span> <span class="n">root</span><span class="p">:</span>
            <span class="n">abs_parts</span> <span class="o">=</span> <span class="p">[</span><span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">]</span> <span class="o">+</span> <span class="n">parts</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">abs_parts</span> <span class="o">=</span> <span class="n">parts</span>
        <span class="n">to_drv</span><span class="p">,</span> <span class="n">to_root</span><span class="p">,</span> <span class="n">to_parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parse_args</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">to_root</span><span class="p">:</span>
            <span class="n">to_abs_parts</span> <span class="o">=</span> <span class="p">[</span><span class="n">to_drv</span><span class="p">,</span> <span class="n">to_root</span><span class="p">]</span> <span class="o">+</span> <span class="n">to_parts</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">to_abs_parts</span> <span class="o">=</span> <span class="n">to_parts</span>
        <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">to_abs_parts</span><span class="p">)</span>
        <span class="n">cf</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">casefold_parts</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">root</span> <span class="ow">or</span> <span class="n">drv</span><span class="p">)</span> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">else</span> <span class="n">cf</span><span class="p">(</span><span class="n">abs_parts</span><span class="p">[:</span><span class="n">n</span><span class="p">])</span> <span class="o">!=</span> <span class="n">cf</span><span class="p">(</span><span class="n">to_abs_parts</span><span class="p">):</span>
            <span class="n">formatted</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_format_parsed_parts</span><span class="p">(</span><span class="n">to_drv</span><span class="p">,</span> <span class="n">to_root</span><span class="p">,</span> <span class="n">to_parts</span><span class="p">)</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{!r}</span><span class="s2"> does not start with </span><span class="si">{!r}</span><span class="s2">&quot;</span>
                             <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">formatted</span><span class="p">)))</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_from_parsed_parts</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">root</span> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
                                       <span class="n">abs_parts</span><span class="p">[</span><span class="n">n</span><span class="p">:])</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">parts</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;An object providing sequence-like access to the</span>
<span class="sd">        components in the filesystem path.&quot;&quot;&quot;</span>
        <span class="c1"># We cache the tuple to avoid building a new one each time .parts</span>
        <span class="c1"># is accessed.  XXX is this necessary?</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pparts</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_pparts</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">)</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pparts</span>

    <span class="k">def</span> <span class="nf">joinpath</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Combine this path with one or several arguments, and return a</span>
<span class="sd">        new path representing either a subpath (if all arguments are relative</span>
<span class="sd">        paths) or a totally different path (if one of the arguments is</span>
<span class="sd">        anchored).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_child</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__truediv__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_child</span><span class="p">((</span><span class="n">key</span><span class="p">,))</span>

    <span class="k">def</span> <span class="fm">__rtruediv__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_from_parts</span><span class="p">([</span><span class="n">key</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">parent</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;The logical parent of the path.&quot;&quot;&quot;</span>
        <span class="n">drv</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_drv</span>
        <span class="n">root</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span>
        <span class="n">parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">parts</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="p">(</span><span class="n">drv</span> <span class="ow">or</span> <span class="n">root</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_from_parsed_parts</span><span class="p">(</span><span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">parts</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">parents</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;A sequence of this path&#39;s logical parents.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_PathParents</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_absolute</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;True if the path is absolute (has both a root and, if applicable,</span>
<span class="sd">        a drive).&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">has_drv</span> <span class="ow">or</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_drv</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_reserved</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if the path contains one of the special names reserved</span>
<span class="sd">        by the system, if any.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">is_reserved</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">match</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path_pattern</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return True if this path matches the given pattern.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cf</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">casefold</span>
        <span class="n">path_pattern</span> <span class="o">=</span> <span class="n">cf</span><span class="p">(</span><span class="n">path_pattern</span><span class="p">)</span>
        <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">pat_parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">parse_parts</span><span class="p">((</span><span class="n">path_pattern</span><span class="p">,))</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">pat_parts</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;empty pattern&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">drv</span> <span class="ow">and</span> <span class="n">drv</span> <span class="o">!=</span> <span class="n">cf</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_drv</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="n">root</span> <span class="ow">and</span> <span class="n">root</span> <span class="o">!=</span> <span class="n">cf</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="n">parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cparts</span>
        <span class="k">if</span> <span class="n">drv</span> <span class="ow">or</span> <span class="n">root</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">pat_parts</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">parts</span><span class="p">):</span>
                <span class="k">return</span> <span class="kc">False</span>
            <span class="n">pat_parts</span> <span class="o">=</span> <span class="n">pat_parts</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">pat_parts</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">parts</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">part</span><span class="p">,</span> <span class="n">pat</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">parts</span><span class="p">),</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">pat_parts</span><span class="p">)):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">fnmatch</span><span class="o">.</span><span class="n">fnmatchcase</span><span class="p">(</span><span class="n">part</span><span class="p">,</span> <span class="n">pat</span><span class="p">):</span>
                <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span>

<span class="c1"># Can&#39;t subclass os.PathLike from PurePath and keep the constructor</span>
<span class="c1"># optimizations in PurePath._parse_args().</span>
<span class="n">os</span><span class="o">.</span><span class="n">PathLike</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">PurePath</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">PurePosixPath</span><span class="p">(</span><span class="n">PurePath</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;PurePath subclass for non-Windows systems.</span>

<span class="sd">    On a POSIX system, instantiating a PurePath should return this object.</span>
<span class="sd">    However, you can also instantiate it directly on any system.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_flavour</span> <span class="o">=</span> <span class="n">_posix_flavour</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="k">class</span> <span class="nc">PureWindowsPath</span><span class="p">(</span><span class="n">PurePath</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;PurePath subclass for Windows systems.</span>

<span class="sd">    On a Windows system, instantiating a PurePath should return this object.</span>
<span class="sd">    However, you can also instantiate it directly on any system.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_flavour</span> <span class="o">=</span> <span class="n">_windows_flavour</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>


<span class="c1"># Filesystem-accessing classes</span>


<span class="k">class</span> <span class="nc">Path</span><span class="p">(</span><span class="n">PurePath</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;PurePath subclass that can make system calls.</span>

<span class="sd">    Path represents a filesystem path but unlike PurePath, also offers</span>
<span class="sd">    methods to do system calls on path objects. Depending on your system,</span>
<span class="sd">    instantiating a Path will return either a PosixPath or a WindowsPath</span>
<span class="sd">    object. You can also instantiate a PosixPath or WindowsPath directly,</span>
<span class="sd">    but cannot instantiate a WindowsPath on a POSIX system or vice versa.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">(</span>
        <span class="s1">&#39;_accessor&#39;</span><span class="p">,</span>
        <span class="s1">&#39;_closed&#39;</span><span class="p">,</span>
    <span class="p">)</span>

    <span class="k">def</span> <span class="fm">__new__</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">cls</span> <span class="ow">is</span> <span class="n">Path</span><span class="p">:</span>
            <span class="bp">cls</span> <span class="o">=</span> <span class="n">WindowsPath</span> <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;nt&#39;</span> <span class="k">else</span> <span class="n">PosixPath</span>
        <span class="bp">self</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_from_parts</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">is_supported</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;cannot instantiate </span><span class="si">%r</span><span class="s2"> on your system&quot;</span>
                                      <span class="o">%</span> <span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_init</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">_init</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
              <span class="c1"># Private non-constructor arguments</span>
              <span class="n">template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
              <span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="n">template</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span> <span class="o">=</span> <span class="n">template</span><span class="o">.</span><span class="n">_accessor</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span> <span class="o">=</span> <span class="n">_normal_accessor</span>

    <span class="k">def</span> <span class="nf">_make_child_relpath</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">part</span><span class="p">):</span>
        <span class="c1"># This is an optimization used for dir walking.  `part` must be</span>
        <span class="c1"># a single part relative to this path.</span>
        <span class="n">parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span> <span class="o">+</span> <span class="p">[</span><span class="n">part</span><span class="p">]</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_from_parsed_parts</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_drv</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">,</span> <span class="n">parts</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="fm">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">tb</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_raise_closed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;I/O operation on closed path&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_opener</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">flags</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="mo">0o666</span><span class="p">):</span>
        <span class="c1"># A stub for the opener argument to built-in open()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flags</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_raw_open</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flags</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="mo">0o777</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Open the file pointed by this path and return a file descriptor,</span>
<span class="sd">        as os.open() does.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flags</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>

    <span class="c1"># Public API</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">cwd</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a new path pointing to the current working directory</span>
<span class="sd">        (as returned by os.getcwd()).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">())</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">home</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a new path pointing to the user&#39;s home directory (as</span>
<span class="sd">        returned by os.path.expanduser(&#39;~&#39;)).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="bp">cls</span><span class="p">()</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">gethomedir</span><span class="p">(</span><span class="kc">None</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">samefile</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other_path</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return whether other_path is the same or not as this file</span>
<span class="sd">        (as returned by os.path.samefile()).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">st</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">other_st</span> <span class="o">=</span> <span class="n">other_path</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="n">other_st</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">other_path</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">samestat</span><span class="p">(</span><span class="n">st</span><span class="p">,</span> <span class="n">other_st</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">iterdir</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Iterate over the files in this directory.  Does not yield any</span>
<span class="sd">        result for the special paths &#39;.&#39; and &#39;..&#39;.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="p">{</span><span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="s1">&#39;..&#39;</span><span class="p">}:</span>
                <span class="c1"># Yielding a path object for these makes little sense</span>
                <span class="k">continue</span>
            <span class="k">yield</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_child_relpath</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">glob</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pattern</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Iterate over this subtree and yield all existing files (of any</span>
<span class="sd">        kind, including directories) matching the given relative pattern.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">pattern</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unacceptable pattern: </span><span class="si">{!r}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">pattern</span><span class="p">))</span>
        <span class="n">pattern</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">casefold</span><span class="p">(</span><span class="n">pattern</span><span class="p">)</span>
        <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">pattern_parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">parse_parts</span><span class="p">((</span><span class="n">pattern</span><span class="p">,))</span>
        <span class="k">if</span> <span class="n">drv</span> <span class="ow">or</span> <span class="n">root</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;Non-relative patterns are unsupported&quot;</span><span class="p">)</span>
        <span class="n">selector</span> <span class="o">=</span> <span class="n">_make_selector</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">pattern_parts</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">selector</span><span class="o">.</span><span class="n">select_from</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">yield</span> <span class="n">p</span>

    <span class="k">def</span> <span class="nf">rglob</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pattern</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Recursively yield all existing files (of any kind, including</span>
<span class="sd">        directories) matching the given relative pattern, anywhere in</span>
<span class="sd">        this subtree.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">pattern</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">casefold</span><span class="p">(</span><span class="n">pattern</span><span class="p">)</span>
        <span class="n">drv</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">pattern_parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">parse_parts</span><span class="p">((</span><span class="n">pattern</span><span class="p">,))</span>
        <span class="k">if</span> <span class="n">drv</span> <span class="ow">or</span> <span class="n">root</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;Non-relative patterns are unsupported&quot;</span><span class="p">)</span>
        <span class="n">selector</span> <span class="o">=</span> <span class="n">_make_selector</span><span class="p">((</span><span class="s2">&quot;**&quot;</span><span class="p">,)</span> <span class="o">+</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pattern_parts</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">selector</span><span class="o">.</span><span class="n">select_from</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">yield</span> <span class="n">p</span>

    <span class="k">def</span> <span class="nf">absolute</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return an absolute version of this path.  This function works</span>
<span class="sd">        even if the path doesn&#39;t point to anything.</span>

<span class="sd">        No normalization is done, i.e. all &#39;.&#39; and &#39;..&#39; will be kept along.</span>
<span class="sd">        Use resolve() to get the canonical path to a file.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># XXX untested yet!</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_absolute</span><span class="p">():</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="c1"># FIXME this must defer to the specific flavour (and, under Windows,</span>
        <span class="c1"># use nt._getfullpathname())</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_from_parts</span><span class="p">([</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">,</span> <span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">_init</span><span class="p">(</span><span class="n">template</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">resolve</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Make the path absolute, resolving all symlinks on the way and also</span>
<span class="sd">        normalizing it (for example turning slashes into backslashes under</span>
<span class="sd">        Windows).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="n">s</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">resolve</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="n">strict</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">s</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># No symlink resolution =&gt; for consistency, raise an error if</span>
            <span class="c1"># the path doesn&#39;t exist or is forbidden</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span>
            <span class="n">s</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">absolute</span><span class="p">())</span>
        <span class="c1"># Now we have no symlinks in the path, it&#39;s safe to normalize it.</span>
        <span class="n">normed</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">pathmod</span><span class="o">.</span><span class="n">normpath</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_from_parts</span><span class="p">((</span><span class="n">normed</span><span class="p">,),</span> <span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">_init</span><span class="p">(</span><span class="n">template</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">stat</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the result of the stat() system call on this path, like</span>
<span class="sd">        os.stat() does.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">owner</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the login name of the file owner.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">import</span> <span class="nn">pwd</span>
        <span class="k">return</span> <span class="n">pwd</span><span class="o">.</span><span class="n">getpwuid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_uid</span><span class="p">)</span><span class="o">.</span><span class="n">pw_name</span>

    <span class="k">def</span> <span class="nf">group</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the group name of the file gid.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">import</span> <span class="nn">grp</span>
        <span class="k">return</span> <span class="n">grp</span><span class="o">.</span><span class="n">getgrgid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_gid</span><span class="p">)</span><span class="o">.</span><span class="n">gr_name</span>

    <span class="k">def</span> <span class="nf">open</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;r&#39;</span><span class="p">,</span> <span class="n">buffering</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
             <span class="n">errors</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">newline</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Open the file pointed by this path and return a file object, as</span>
<span class="sd">        the built-in open() function does.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">io</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="p">,</span> <span class="n">buffering</span><span class="p">,</span> <span class="n">encoding</span><span class="p">,</span> <span class="n">errors</span><span class="p">,</span> <span class="n">newline</span><span class="p">,</span>
                       <span class="n">opener</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_opener</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">read_bytes</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Open the file in bytes mode, read it, and close the file.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">read_text</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Open the file in text mode, read it, and close the file.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s1">&#39;r&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="n">errors</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">write_bytes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Open the file in bytes mode, write to it, and close the file.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># type-check for the buffer interface before truncating the file</span>
        <span class="n">view</span> <span class="o">=</span> <span class="nb">memoryview</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">view</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">write_text</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Open the file in text mode, write to it, and close the file.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;data must be str, not </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                            <span class="n">data</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="n">errors</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">touch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="mo">0o666</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create this file with the given access mode, if it doesn&#39;t exist.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">exist_ok</span><span class="p">:</span>
            <span class="c1"># First try to bump modification time</span>
            <span class="c1"># Implementation note: GNU touch uses the UTIME_NOW option of</span>
            <span class="c1"># the utimensat() / futimens() functions.</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">utime</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
                <span class="c1"># Avoid exception chaining</span>
                <span class="k">pass</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span>
        <span class="n">flags</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">O_CREAT</span> <span class="o">|</span> <span class="n">os</span><span class="o">.</span><span class="n">O_WRONLY</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">exist_ok</span><span class="p">:</span>
            <span class="n">flags</span> <span class="o">|=</span> <span class="n">os</span><span class="o">.</span><span class="n">O_EXCL</span>
        <span class="n">fd</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_raw_open</span><span class="p">(</span><span class="n">flags</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>
        <span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">mkdir</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="mo">0o777</span><span class="p">,</span> <span class="n">parents</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a new directory at this given path.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">FileNotFoundError</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">parents</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="o">==</span> <span class="bp">self</span><span class="p">:</span>
                <span class="k">raise</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">parents</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="n">parents</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="n">exist_ok</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
            <span class="c1"># Cannot rely on checking for EEXIST, since the operating system</span>
            <span class="c1"># could give priority to other errors like EACCES or EROFS</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">exist_ok</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_dir</span><span class="p">():</span>
                <span class="k">raise</span>

    <span class="k">def</span> <span class="nf">chmod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Change the permissions of the path, like os.chmod().</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">chmod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">lchmod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Like chmod(), except if the path points to a symlink, the symlink&#39;s</span>
<span class="sd">        permissions are changed, rather than its target&#39;s.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">lchmod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">unlink</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Remove this file or link.</span>
<span class="sd">        If the path is a directory, use rmdir() instead.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">rmdir</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Remove this directory.  The directory must be empty.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">rmdir</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">lstat</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Like stat(), except if the path points to a symlink, the symlink&#39;s</span>
<span class="sd">        status information is returned, rather than its target&#39;s.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">lstat</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">rename</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Rename this path to the given path.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">replace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Rename this path to the given path, clobbering the existing</span>
<span class="sd">        destination if it exists.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">symlink_to</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target</span><span class="p">,</span> <span class="n">target_is_directory</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Make this path a symlink pointing to the given path.</span>
<span class="sd">        Note the order of arguments (self, target) is the reverse of os.symlink&#39;s.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_closed</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_raise_closed</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_accessor</span><span class="o">.</span><span class="n">symlink</span><span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">target_is_directory</span><span class="p">)</span>

    <span class="c1"># Convenience functions for querying the stat results</span>

    <span class="k">def</span> <span class="nf">exists</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Whether this path exists.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">_ignore_error</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
                <span class="k">raise</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">is_dir</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Whether this path is a directory.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">S_ISDIR</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_mode</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">_ignore_error</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
                <span class="k">raise</span>
            <span class="c1"># Path doesn&#39;t exist or is a broken symlink</span>
            <span class="c1"># (see https://bitbucket.org/pitrou/pathlib/issue/12/)</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">is_file</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Whether this path is a regular file (also True for symlinks pointing</span>
<span class="sd">        to regular files).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">S_ISREG</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_mode</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">_ignore_error</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
                <span class="k">raise</span>
            <span class="c1"># Path doesn&#39;t exist or is a broken symlink</span>
            <span class="c1"># (see https://bitbucket.org/pitrou/pathlib/issue/12/)</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">is_mount</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check if this path is a POSIX mount point</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Need to exist and be a dir</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_dir</span><span class="p">():</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="n">parent</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">parent_dev</span> <span class="o">=</span> <span class="n">parent</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_dev</span>
        <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="n">dev</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_dev</span>
        <span class="k">if</span> <span class="n">dev</span> <span class="o">!=</span> <span class="n">parent_dev</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">ino</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_ino</span>
        <span class="n">parent_ino</span> <span class="o">=</span> <span class="n">parent</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_ino</span>
        <span class="k">return</span> <span class="n">ino</span> <span class="o">==</span> <span class="n">parent_ino</span>

    <span class="k">def</span> <span class="nf">is_symlink</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Whether this path is a symbolic link.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">S_ISLNK</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lstat</span><span class="p">()</span><span class="o">.</span><span class="n">st_mode</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">_ignore_error</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
                <span class="k">raise</span>
            <span class="c1"># Path doesn&#39;t exist</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">is_block_device</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Whether this path is a block device.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">S_ISBLK</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_mode</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">_ignore_error</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
                <span class="k">raise</span>
            <span class="c1"># Path doesn&#39;t exist or is a broken symlink</span>
            <span class="c1"># (see https://bitbucket.org/pitrou/pathlib/issue/12/)</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">is_char_device</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Whether this path is a character device.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">S_ISCHR</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_mode</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">_ignore_error</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
                <span class="k">raise</span>
            <span class="c1"># Path doesn&#39;t exist or is a broken symlink</span>
            <span class="c1"># (see https://bitbucket.org/pitrou/pathlib/issue/12/)</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">is_fifo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Whether this path is a FIFO.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">S_ISFIFO</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_mode</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">_ignore_error</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
                <span class="k">raise</span>
            <span class="c1"># Path doesn&#39;t exist or is a broken symlink</span>
            <span class="c1"># (see https://bitbucket.org/pitrou/pathlib/issue/12/)</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">is_socket</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Whether this path is a socket.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">S_ISSOCK</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_mode</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">_ignore_error</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
                <span class="k">raise</span>
            <span class="c1"># Path doesn&#39;t exist or is a broken symlink</span>
            <span class="c1"># (see https://bitbucket.org/pitrou/pathlib/issue/12/)</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">expanduser</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Return a new path with expanded ~ and ~user constructs</span>
<span class="sd">        (as returned by os.path.expanduser)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_drv</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_root</span><span class="p">)</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">[</span><span class="mi">0</span><span class="p">][:</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;~&#39;</span><span class="p">):</span>
            <span class="n">homedir</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flavour</span><span class="o">.</span><span class="n">gethomedir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">:])</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_from_parts</span><span class="p">([</span><span class="n">homedir</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parts</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>

        <span class="k">return</span> <span class="bp">self</span>


<span class="k">class</span> <span class="nc">PosixPath</span><span class="p">(</span><span class="n">Path</span><span class="p">,</span> <span class="n">PurePosixPath</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Path subclass for non-Windows systems.</span>

<span class="sd">    On a POSIX system, instantiating a Path should return this object.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

<span class="k">class</span> <span class="nc">WindowsPath</span><span class="p">(</span><span class="n">Path</span><span class="p">,</span> <span class="n">PureWindowsPath</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Path subclass for Windows systems.</span>

<span class="sd">    On a Windows system, instantiating a Path should return this object.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">()</span>

    <span class="k">def</span> <span class="nf">owner</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;Path.owner() is unsupported on this system&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">group</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;Path.group() is unsupported on this system&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_mount</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;Path.is_mount() is unsupported on this system&quot;</span><span class="p">)</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>